package es.ats.codington.festival.services;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import es.ats.codington.festival.daos.PlaceDAOImpl;
import es.ats.codington.festival.domain.Building;
import es.ats.codington.festival.domain.LargeBusiness;
import es.ats.codington.festival.domain.Museum;
import es.ats.codington.festival.domain.Park;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.domain.Stadium;
import es.ats.codington.festival.domain.Theater;
import es.ats.codington.festival.domain.TouristAttraction;
import es.ats.codington.festival.domain.TraditionalMarket;
import es.ats.codington.festival.domain.Zoo;
import es.ats.codington.festival.domain.Place.Real;
import es.ats.codington.festival.domain.Place.Zone;
import es.ats.codington.festival.interfaces.services.IPlaceService;
import es.ats.codington.web.servlets.CreatePlaceServlet.TypeName;

public class PlaceServiceImpl implements IPlaceService{
		
	/**
	 * Show place all.
	 *
	 * @return true, if successful
	 * @see es.ats.codington.festival.interfaces.services.IPlaceService#ShowPlaceAll()
	 */
	public boolean ShowPlaceAll(){
		Connection con = null;
		boolean rs = false;
		
		ServiceManager serviceManager = new ServiceManager();
		PlaceDAOImpl placeDAO = new PlaceDAOImpl();
		try{
			con = serviceManager.OpenConnectionToDB();			
			rs = placeDAO.showAllPlace(con);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		//Printing how many rows got added
		/**if (rs) System.out.println("correct show!!");*/
		return rs;
	}
	
	/**
	 * Obtain all places.
	 *
	 * @return the array list
	 */
	public ArrayList<Integer> obtainAllPlaces() {
		Connection con = null;
		ServiceManager serviceManager = new ServiceManager();
		PlaceDAOImpl placeDAO = new PlaceDAOImpl();
		ArrayList<Integer> pla = new ArrayList<Integer>();
		try{
			con = serviceManager.OpenConnectionToDB();			
			pla = placeDAO.obtainAllPlaces(con);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return pla;
	}
	
	public HashMap<Integer, String> obtainNameAndId(){
		HashMap<Integer, String> myPlaces = new HashMap<Integer, String>();
		
		Connection con = null;
		ServiceManager serviceManager = new ServiceManager();
		PlaceDAOImpl placeDAO = new PlaceDAOImpl();

		try{
			con = serviceManager.OpenConnectionToDB();			
			myPlaces = placeDAO.obtainPlacesNameId(con);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return myPlaces;
	}
	
	
	/**
	 * Show places id.
	 *
	 * @param id the id
	 * @return true, if successful
	 */
	public boolean ShowPlacesId(int id){
		Connection con = null;
		boolean rs = false;
		
		ServiceManager serviceManager = new ServiceManager();
		PlaceDAOImpl placeDAO = new PlaceDAOImpl();
		try{
			con = serviceManager.OpenConnectionToDB();			
			rs = placeDAO.showPlacesId(con, id);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		//Printing how many rows got added
		if (rs) System.out.println("correct show!!");
		return rs;
	}
	
	
	//Inserts
	
	/**
	 * Register place.
	 *
	 * @param place the place
	 * @return true, if successful
	 * @throws Exception the exception
	 */
	public boolean registerPlace (Place place) throws Exception{
		Connection con = null;
		boolean rs = false;
		
		ServiceManager serviceManager = new ServiceManager();
		PlaceDAOImpl placeDAO = new PlaceDAOImpl();
		try{
			con = serviceManager.OpenConnectionToDB();			
			rs = placeDAO.insertPlaceDAO(con, place);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		//Printing how many rows got added
		if (rs) System.out.println(" Place added!");
		return rs;
	}
	
	/**
	 * Register building.
	 *
	 * @param building the building
	 * @return true, if successful
	 * @throws Exception the exception
	 */
	public boolean registerBuilding (Building building) throws Exception{
		Connection con = null;
		boolean rs = false;
		
		ServiceManager serviceManager = new ServiceManager();
		PlaceDAOImpl placeDAO = new PlaceDAOImpl();
		try{
			con = serviceManager.OpenConnectionToDB();			
			rs = placeDAO.insertPlaceDAO(con, building);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		//Printing how many rows got added
		if (rs) System.out.println(" Building added!");
		return rs;
	}
	
	//Deletes
	
		/**
	 * Delete place.
	 *
	 * @param place the place
	 * @return true, if successful
	 * @throws Exception the exception
	 */
	public boolean deletePlace (Place place) throws Exception{
			Connection con = null;
			boolean rs = false;
			
			ServiceManager serviceManager = new ServiceManager();
			PlaceDAOImpl placeDAO = new PlaceDAOImpl();
			try{
				con = serviceManager.OpenConnectionToDB();			
				rs = placeDAO.deletePlaceDAO(con, place);
				serviceManager.CloseConnectionToDB(con);
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
			//Printing how many rows got added
			if (rs) System.out.println("Place delete!");
			return rs;
		}
		
		/**
		 * Delete building.
		 *
		 * @param building the building
		 * @return true, if successful
		 * @throws Exception the exception
		 */
		public boolean deleteBuilding (Building building) throws Exception{
			Connection con = null;
			boolean rs = false;
			
			ServiceManager serviceManager = new ServiceManager();
			PlaceDAOImpl placeDAO = new PlaceDAOImpl();
			try{
				con = serviceManager.OpenConnectionToDB();			
				rs = placeDAO.deletePlaceDAO(con, building);
				serviceManager.CloseConnectionToDB(con);
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
			//Printing how many rows got added
			if (rs) System.out.println(" Building delete!");
			return rs;
		}
		
		//Updates
		
		/**
		 * Update place.
		 *
		 * @param place the place
		 * @return true, if successful
		 * @throws Exception the exception
		 */
		public boolean updatePlace (Place place) throws Exception{
			Connection con = null;
			boolean rs = false;
			
			ServiceManager serviceManager = new ServiceManager();
			PlaceDAOImpl placeDAO = new PlaceDAOImpl();
			try{
				con = serviceManager.OpenConnectionToDB();			
				rs = placeDAO.updatePlaceDAO(con, place);
				serviceManager.CloseConnectionToDB(con);
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
			//Printing how many rows got added
			if (rs) System.out.println("Place update!");
			return rs;
		}
		
		/**
		 * Update building.
		 *
		 * @param building the building
		 * @return true, if successful
		 * @throws Exception the exception
		 */
		public boolean updateBuilding (Building building) throws Exception{
			Connection con = null;
			boolean rs = false;
			
			ServiceManager serviceManager = new ServiceManager();
			PlaceDAOImpl placeDAO = new PlaceDAOImpl();
			try{
				con = serviceManager.OpenConnectionToDB();			
				rs = placeDAO.updatePlaceDAO(con, building);
				serviceManager.CloseConnectionToDB(con);
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
			//Printing how many rows got added
			if (rs) System.out.println("Building update!");
			return rs;
		}
		
		/**
		 * Sets the place.
		 *
		 * @param request the request
		 * @return true, if successful
		 * @throws Exception the exception
		 * @see es.ats.codington.festival.interfaces.services.IPlaceService#setPlace(javax.servlet.http.HttpServletRequest)
		 */
		public boolean setPlace (HttpServletRequest request)throws Exception{
			
			Zone tow;
			boolean aux=true;
			
			PlaceServiceImpl pla = new PlaceServiceImpl();
						       
            String name = request.getParameter("placename");
            String description = request.getParameter("description");
            String zone = request.getParameter("zone");
            String type = request.getParameter("type");
            
            
            String capacity = request.getParameter("capacity");
            
            int capac = Integer.parseInt(capacity);   
                        
            
            if(zone.equals("North Tower")){
            	tow=Zone.north;
            }
            else{
            	tow=Zone.south;
            }
            
        
            switch (TypeName.valueOf(type)) {
			case zoo:
				Zoo zoo = new Zoo();
            	
            	zoo.setzName(name);
				zoo.setPlaceName(name);
				zoo.setPlaceDescription(description);
				zoo.setZone(tow);
				zoo.setCapacity(capac);
				zoo.setReal(Real.zoo);
				
				aux= pla.registerPlace(zoo);
				break;
			case attraction:
				TouristAttraction atraction = new TouristAttraction();
				
				atraction.setaName(name);
				atraction.setPlaceName(name);
				atraction.setPlaceDescription(description);
				atraction.setZone(tow);
				atraction.setCapacity(capac);
				atraction.setReal(Real.touristattraction);
				
				aux=pla.registerPlace(atraction);
				break;

				
			case park:
				Park park = new Park();
				
				park.setPlaceName(name);
				park.setpName(name);
				park.setPlaceDescription(description);
				park.setCapacity(capac);
				park.setZone(tow);
				park.setReal(Real.park);
				
				aux=pla.registerPlace(park);
				break;

				
			case market:
				TraditionalMarket mark = new TraditionalMarket();
				
				mark.setMarketName(name);
				mark.setPlaceName(name);
				mark.setPlaceDescription(description);
				mark.setCapacity(capac);
				mark.setZone(tow);
				mark.setReal(Real.traditionalmarket);
				
				aux=pla.registerBuilding(mark);
				break;
				
			case theater:
				Theater the = new Theater();
				
				the.setPlaceName(name);
				the.setTheaterName(name);
				the.setPlaceDescription(description);
				the.setCapacity(capac);
				the.setZone(tow);
				the.setReal(Real.theater);
				
				aux=pla.registerBuilding(the);
				break;
				
			case stadium:
				Stadium sta = new Stadium();
				
				sta.setPlaceName(name);
				sta.setStadiumName(name);
				sta.setPlaceDescription(description);
				sta.setCapacity(capac);
				sta.setZone(tow);
				sta.setReal(Real.stadium);
				
				aux=pla.registerBuilding(sta);
				break;

				
			case museum:
				Museum mus = new Museum();
				
				mus.setPlaceName(name);
				mus.setmName(name);
				mus.setPlaceDescription(description);
				mus.setCapacity(capac);
				mus.setZone(tow);
				mus.setReal(Real.museum);
				
				aux=pla.registerBuilding(mus);
				break;

				
			case busines:
				LargeBusiness busines = new LargeBusiness();
				
				busines.setPlaceName(name);
				busines.setBusinessName(name);
				busines.setPlaceDescription(description);
				busines.setCapacity(capac);
				busines.setZone(tow);
				busines.setReal(Real.busines);
				
				aux=pla.registerBuilding(busines);
				break;


			default:
				break;
			}
            
            return aux;
		}	
}

